package cn.hist.hfp.converter;

import cn.hist.hfp.entity.CommodityOrder;
import cn.hist.hfp.vo.CommodityOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class CommodityOrderConverter {

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final CommodityConverter commodityConverter;

    @Autowired
    public CommodityOrderConverter(CommodityConverter commodityConverter) {
        this.commodityConverter = commodityConverter;
    }

    public CommodityOrderVO convert(CommodityOrder order) {
        return CommodityOrderVO.builder().amount(order.getAmount())
                .id(order.getId())
                .transactionDate(formatter.format(order.getTransactionDate()))
                .userId(order.getUser().getId())
                .userName(order.getUser().getName())
                .commodity(commodityConverter.converter(order.getCommodity())).build();
    }

    public List<CommodityOrderVO> convert(List<CommodityOrder> commodityOrders) {
        List<CommodityOrderVO> commodityOrderVOS = new ArrayList<>();
        for (CommodityOrder commodityOrder: commodityOrders) {
            commodityOrderVOS.add(convert(commodityOrder));
        }
        return commodityOrderVOS;
    }

    public Set<CommodityOrderVO> convert(Set<CommodityOrder> commodityOrders) {
        Set<CommodityOrderVO> commodityOrderVOS = new HashSet<>();
        for (CommodityOrder commodityOrder: commodityOrders) {
            commodityOrderVOS.add(convert(commodityOrder));
        }
        return commodityOrderVOS;
    }
}
